#include "Debug.h"
#include "Cylinder.h"


Cylinder::Cylinder(BoundingBox* boundingBox)
{
	bb = boundingBox;
	height = bb->topLine - bb->bottomLine;
	centre.x = 0.5*bb->rightLine + 0.5*bb->leftLine;
	centre.z = 0.5*bb->frontLine + 0.5*bb->backLine;
	radius = max(bb->rightLine - centre.x, bb->frontLine - centre.z);
	circ = 2*M_PI*radius;
	setUpBounds();

	startVector = NULL;
}


Cylinder::~Cylinder(void)
{
}

void Cylinder::getUnscaledUVs(MPoint coords, float2 outUVs)
{	
	double o = (coords.z - centre.z);
	double a = (coords.x - centre.x);
	double tanVal =  o/a;
	
	outUVs[0] = (float) (atan(tanVal) * 180 / M_PI);
	if(o >= 0 && a >= 0)
		outUVs[0] = outUVs[0];
	else if(o>0 && a < 0)
		outUVs[0] = 180 + outUVs[0];
	else if(o<0 && a < 0)
		outUVs[0] = outUVs[0] + 180;
	else if(o<0 && a>0)
		outUVs[0] = 360 + outUVs[0];

	outUVs[1] = (float)(coords.y-bb->bottomLine);
	//cout << coords.x << "," << coords.y << "," << coords.z << "\tTanval: " << tanVal << "\tAngle: " << outUVs[0] << endl;
	updateBounds(outUVs);
}

double Cylinder::getWidthToHeightRatio()
{
	return circ == height ? 1 : abs(height)/abs(circ);
}
